﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DAO;
namespace giaodien
{
    public partial class dichvu : Form
    {
        public dichvu()
        {
            InitializeComponent();
        }
        int key = -1;
        public void datphong_Load(object sender, EventArgs e)
        {
            combotendv.Enabled = false;
            txtmakh.ReadOnly = true;
            txtsl.ReadOnly = true;
            txtthanhtien.ReadOnly = true;
            datengaydat.Enabled = false;
            DataSet ds = new DataSet();
            ds = dichvu_BUS.loadtendv();
            combotendv.DataSource = ds.Tables[0];
            combotendv.DisplayMember = "tendv";
            combotendv.ValueMember = "madv";
            combotendv.SelectedIndex = -1;
            lbldongia.Text = "";
            lblmadv.Text = "";
            ds = new DataSet();
            ds = dichvu_BUS.loadmp();
            combomap.DataSource = ds.Tables[0];
            combomap.DisplayMember = "map";
            combomap.ValueMember = "map";
            combomap.SelectedIndex = -1;
            lbldvtinh.Text = "";
            txtmakh.Text = "";
            listdichvu.Items.Clear();
        }

        private void dichvu_MouseDown(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseDown(e);
        }

        private void dichvu_MouseMove(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseMove(this, MousePosition.X, MousePosition.Y);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            GDChinh frm = new GDChinh();
            DialogResult d = MessageBox.Show("Bạn Có Muốn Thoát", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                this.Close();
                frm.Show();
            }
        }

        private void dichvu_MouseUp(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseUp();
        }

        private void nutthoat_Click(object sender, EventArgs e)
        {
            GDChinh frm = new GDChinh();
            DialogResult d = MessageBox.Show("Bạn có muốn thoát", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                this.Close();
                frm.Show();
            }
        }

        private void txtsl_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
            {
                e.Handled = true;
            }
        }

        void hienthithongtinlistdv(DataSet ds, ListView danhsach)
        {
            listdichvu.Items.Clear();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lvw = new ListViewItem();
                lvw.Text = ds.Tables[0].Rows[i][0].ToString();
                for (int j = 1; j < ds.Tables[0].Columns.Count; j++)
                {
                      lvw.SubItems.Add(ds.Tables[0].Rows[i][j].ToString());
                }
                danhsach.Items.Add(lvw);
            }
        }
        void laysukienkhimousedown(ListView danhsachmousedown, object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
            {
                ListViewItem lvw = listdichvu.GetItemAt(e.X, e.Y);
                if (lvw != null)
                {
                    combotendv.Text = lvw.SubItems[1].Text;
                    combomap.Text = lvw.SubItems[2].Text;
                    txtmakh.Text = lvw.SubItems[3].Text;
                    txtthanhtien.Text = lvw.SubItems[6].Text;
                    txtsl.Text = lvw.SubItems[4].Text;
                    datengaydat.Text = lvw.SubItems[5].Text;
                }
            }
        }
        public string tinhtongtien(ListView l)
        {
            double dTongtien = 0;
            foreach (ListViewItem lvi in l.Items)
            {
                dTongtien += double.Parse(lvi.SubItems[6].Text);     
            }
            return Convert.ToString(dTongtien);
        }
        private void nutthemdv_Click(object sender, EventArgs e)
        {
            groupBox2.Visible = true;
            //lbltongtien.Visible = true;
            key = 0;
            combotendv.Enabled = true;
            combomap.Enabled = true;
            txtsl.ReadOnly = false;
            datengaydat.Enabled = true;
            lblmadv.Text = "";
            datengaydat.Text = "";
            combotendv.Text = "";
            combomap.Text = "";
            txtmakh.Text="";
            lbldongia.Text = "";
            txtsl.Text = "";
            txtthanhtien.Text = "";
            lbldvtinh.Text = "";
        }
        private void combomap_SelectedValueChanged_1(object sender, EventArgs e)
        {
            if (!combomap.Text.Equals(""))
            {
                    DataRowView rowmap = combomap.SelectedItem as DataRowView;
                    txtmakh.Text = rowmap.Row.ItemArray.GetValue(0).ToString();
                    DataSet ds = new DataSet();
                    hoadondv_DTO hddv = new hoadondv_DTO();
                    hddv.makh = txtmakh.Text;
                    ds = dichvu_BUS.loadthongtinhoadondvvaolistview(hddv);
                    hienthithongtinlistdv(ds, listdichvu);
                    lbltongtien.Text = "Tổng tiền: " + tinhtongtien(listdichvu)+" VNĐ";              
            }
        }
        private void combotendv_SelectedValueChanged_1(object sender, EventArgs e)
        {
            if (combotendv.SelectedIndex >= 0)
            {
                DataRowView row = combotendv.SelectedItem as DataRowView;
                lblmadv.Text = row.Row.ItemArray.GetValue(0).ToString();
                lbldongia.Text = row.Row.ItemArray.GetValue(2).ToString();
            }
        }
        private void combotendv_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (combotendv.Text == "")
                lbldvtinh.Text = "";
            if (combotendv.SelectedIndex == 0)
                lbldvtinh.Text = "Kg";
            if (combotendv.SelectedIndex == 1)
                lbldvtinh.Text = "Vé";
            if (combotendv.SelectedIndex == 2)
                lbldvtinh.Text = "Vé";
            if (combotendv.SelectedIndex == 3)
                lbldvtinh.Text = "Vé";
        }
        int thanhtien, dg;
        private void nuttinh_Click_1(object sender, EventArgs e)
        {
            int sl;
            if (txtsl.Text == "")
                sl = 0;
            else
                sl = Convert.ToInt32(txtsl.Text);
                dg = Convert.ToInt32(lbldongia.Text);
                thanhtien = dg * sl;         
                txtthanhtien.Text = thanhtien + "";
        }

        private void nutxoadv_Click(object sender, EventArgs e)
        {      
            foreach (ListViewItem lvw in listdichvu.SelectedItems)
            {
                if (lvw != null)
                {
                    hoadondv_DTO hddv = new hoadondv_DTO();
                    hddv.mahddv = lvw.Text;
                    hddv.makh = lvw.SubItems[3].Text;
                    DialogResult d = MessageBox.Show("Bạn có chắc muốn xóa", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (d == DialogResult.Yes)
                    {
                        if (dichvu_BUS.xoadv(hddv) == true)
                        {                           
                            MessageBox.Show("Xóa thành công");
                            DataSet ds = new DataSet();
                            ds = dichvu_BUS.loadthongtinhoadondvvaolistview(hddv);
                            hienthithongtinlistdv(ds, listdichvu);
                            Text = "Tổng tiền: " + tinhtongtien(listdichvu)+" VNĐ";
                        }
                        else
                        {
                            MessageBox.Show("Xóa không thành công");
                        }
                    }
                }
            }
        }
        public string layGioPhuGiay(string sGioPhut)
        {
            sGioPhut = "";
            int Gio, Phut;
            Gio = DateTime.Now.Hour;
            Phut = DateTime.Now.Minute;
            return Convert.ToString(Gio + "/" + Phut);
        }
        private void nutluu_Click(object sender, EventArgs e)
        {
            if (key == 0)
            {
                if ((combotendv.Text == "") || (combomap.Text == "") || (txtsl.Text == ""))
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin");
                else
                {
                    if (txtthanhtien.Text == "")
                        MessageBox.Show("Vui lòng tính tiền trước khi thêm");
                    else
                    {
                        DataSet ds = new DataSet();
                        hoadondv_DTO hddv = new hoadondv_DTO();
                        hddv.madv = combotendv.SelectedValue.ToString();
                        hddv.map = int.Parse(combomap.SelectedValue.ToString());
                        hddv.makh = txtmakh.Text;
                        hddv.soluong = int.Parse(txtsl.Text);
                        //string SGioPhut="";
                        //SGioPhut=layGioPhuGiay(SGioPhut);
                        hddv.ngaydat = DateTime.Parse(datengaydat.Value.ToString());
                        hddv.thanhtien = thanhtien;
                        if (dichvu_BUS.themdv(hddv) == true)
                        {
                            MessageBox.Show("Thêm thành công", "Thông báo");
                            ds = dichvu_BUS.loadthongtinhoadondvvaolistview(hddv);
                            hienthithongtinlistdv(ds, listdichvu);
                            lbltongtien.Text = "Tổng tiền: " + tinhtongtien(listdichvu) + " VNĐ";
                            combotendv.Enabled = false;
                            combomap.Enabled = true;
                            txtmakh.ReadOnly = true;
                            txtsl.ReadOnly = true;
                            datengaydat.Enabled = false;
                        }
                        else
                        {
                            MessageBox.Show("Thêm không thành công", "Thông báo");
                        }
                    }
                }
            }
            if (key == 1)
            {
                if ((combotendv.Text == "") || (combomap.Text == "") || (txtsl.Text == ""))
                    MessageBox.Show("Vui lòng điền đầy đủ thông tin");
                else
                {
                    foreach (ListViewItem lvw in listdichvu.SelectedItems)
                    {
                        if (txtsl.Text != lvw.SubItems[4].Text)
                        {
                            if (txtthanhtien.Text == lvw.SubItems[6].Text)
                                MessageBox.Show("Vui lòng tính lại số tiền");
                            else
                            {
                                DataSet ds = new DataSet();
                                hoadondv_DTO hddv = new hoadondv_DTO();
                                hddv.map = int.Parse(combomap.SelectedValue.ToString());
                                hddv.mahddv = lvw.Text;
                                //hddv.map = int.Parse(combomap.Text);
                                hddv.soluong = int.Parse(txtsl.Text);
                                hddv.ngaydat = DateTime.Parse(datengaydat.Text);
                                hddv.thanhtien = int.Parse(txtthanhtien.Text);
                                hddv.madv = combotendv.SelectedValue.ToString();
                                DialogResult d = MessageBox.Show("Bạn có muốn sửa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                                if (d == DialogResult.Yes)
                                {
                                    if (dichvu_BUS.suadv(hddv) == true)
                                    {
                                        MessageBox.Show("Sửa thành công");
                                        hddv.makh = txtmakh.Text;
                                        ds = dichvu_BUS.loadthongtinhoadondvvaolistview(hddv);
                                        hienthithongtinlistdv(ds, listdichvu);
                                        lbltongtien.Text = "Tổng tiền: " + tinhtongtien(listdichvu) + " VNĐ";
                                        combotendv.Enabled = false;
                                        combomap.Enabled = true;
                                        txtmakh.ReadOnly = true;
                                        txtsl.ReadOnly = true;
                                        datengaydat.Enabled = false;
                                    }
                                    else
                                        MessageBox.Show("Sửa không thành công");
                                }
                            }
                        }
                    }
                }
            }
        }

        private void listdichvu_MouseDown(object sender, MouseEventArgs e)
        {
            laysukienkhimousedown(listdichvu, sender, e);
            groupBox2.Visible = true;
            //lbltongtien.Visible = true;
        }

        private void nutsuadv_Click(object sender, EventArgs e)
        {
            key = 1;
            combotendv.Enabled = true;
            combomap.Enabled = false;
            txtsl.ReadOnly = false;
            datengaydat.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form frm = new themxoasuadichvu();
            frm.Show();
        }

        private void combotendv_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void combomap_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }
    }
}
